package com.goodflower.goodflower.chang_mac;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.goodflower.goodflower.FlowerActivity;
import com.goodflower.goodflower.R;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Change_myflower_info extends AppCompatActivity {

    private String flower_mac;
    private EditText change_mac_name;
    private EditText change_mac_kongshi;
    private EditText change_mac_kongwen;
    private EditText change_mac_jt;
    private EditText change_mac_pt;
    private Button btn_chang_mac;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_mac_info);
        //获取上一个页面的参数　　获取mac
        Intent from_FlowerAdapter_intent = getIntent();
        flower_mac = from_FlowerAdapter_intent.getStringExtra("flower_mac");
        //找到控件
        change_mac_name = (EditText) findViewById(R.id.change_mac_name);
        change_mac_kongshi = (EditText) findViewById(R.id.change_mac_kongshi);
        change_mac_kongwen = (EditText) findViewById(R.id.change_mac_kongwen);
        change_mac_jt = (EditText) findViewById(R.id.change_mac_jt);
        change_mac_pt = (EditText) findViewById(R.id.change_mac_pt);
        btn_chang_mac = (Button)findViewById(R.id.btn_chang_mac);
        btn_chang_mac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.sendOkHttpRequest("http://"+Info.server_ip+"/huahua/phone_setmacinfo.php?" +
                        "kongshi="+change_mac_kongshi.getText().toString()+"&kongwen="+change_mac_kongwen.getText().toString()+
                        "&jt="+change_mac_jt.getText().toString()+
                        "&pt="+change_mac_pt.getText().toString()+"&" +
                        "macname="+change_mac_name.getText().toString()+
                        "&mac="+flower_mac, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //Toast.makeText(Change_myflower_info.this,"设置成功",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(Change_myflower_info.this, FlowerActivity.class);
                        intent.putExtra("yourname", Info.current_user_name);
                        startActivity(intent);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Change_myflower_info.this,"设置成功",Toast.LENGTH_LONG).show();
                            }
                        });
                        finish();
                    }
                });
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendOkHttpRequest("http://"+Info.server_ip+"/huahua/phone_getmacinfo.php?mac=" + flower_mac, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(Change_myflower_info.this, "请求错误", Toast.LENGTH_LONG).show();
                        Log.d("urlerror", "请求错误");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res = response.body().string();
                        try {
                            JSONObject jsonObject = new JSONObject(res);
                            final String kongshi = jsonObject.getString("kongshi");
                            final String kongwen = jsonObject.getString("kongwen");
                            final String jt = jsonObject.getString("jt");
                            final String pt = jsonObject.getString("pt");
                            final String macname = jsonObject.getString("macname");

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    change_mac_kongshi.setText(kongshi);
                                    change_mac_kongwen.setText(kongwen);
                                    change_mac_jt.setText(jt);
                                    change_mac_pt.setText(pt);
                                    change_mac_name.setText(macname);
                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });

            }
        }).start();
    }
}
